package com.jdd.modules.parkmonthly.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.common.api.vo.Result;
import com.jdd.common.system.vo.LoginUser;
import com.jdd.common.util.LoginUserUtils;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.modules.area.entity.ParkAreaInfo;
import com.jdd.modules.area.service.IParkAreaInfoService;
import com.jdd.modules.parking.entity.ParkInfo;
import com.jdd.modules.parking.service.IParkInfoService;
import com.jdd.modules.parkmonthly.entity.PermissionArea;
import com.jdd.modules.parkmonthly.entity.PermissionGroup;
import com.jdd.modules.parkmonthly.mapper.PermissionAreaMapper;
import com.jdd.modules.parkmonthly.service.IPermissionAreaService;
import com.jdd.modules.parkmonthly.service.IPermissionGroupService;


/**
 * @Description: 权限-区域关联表
 * @Author: jdd
 * @Date:   2021-11-09
 * @Version: V1.0
 */
@Service
public class PermissionAreaServiceImpl extends ServiceImpl<PermissionAreaMapper, PermissionArea> implements IPermissionAreaService {
    @Resource
    private IParkAreaInfoService parkAreaInfoService;
    @Resource
    private IParkInfoService parkInfoService;
    @Resource
    @Lazy
    private IPermissionGroupService permissionGroupService;

    @Override
    public Result addByIdList(List<String> idList,String permissionName) {
        LoginUser loginUser = LoginUserUtils.getLoginUser();
        try {
            List<PermissionGroup> list = new ArrayList<>();
            for (String id:idList){
                ParkAreaInfo parkAreaInfo = parkAreaInfoService.getById(id);
                ParkInfo parkInfo = parkInfoService.getParkInfoByParkCode();
                PermissionGroup permissionGroup = new PermissionGroup();
                permissionGroup.setId(SnowFlakeUtil.getId());
                permissionGroup.setParkCode(parkAreaInfo.getParkCode());
                permissionGroup.setParkName(parkInfo.getParkName());
                permissionGroup.setPermissionName(permissionName);
                permissionGroup.setCreateUserId(loginUser.getUsername());
                permissionGroup.setCreateUserName(loginUser.getUsername());
                list.add(permissionGroup);
            }
            permissionGroupService.saveBatch(list);
            Result.ok("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            Result.error("操作失败");
        }
        return null;
    }


}
